package com.example.a22310.myapplication.ViewModel.User;


import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.Toast;

import com.example.a22310.myapplication.Adapter.MvvmAdapter;
import com.example.a22310.myapplication.Beans.Post;
import com.example.a22310.myapplication.Listener.PostListener;
import com.example.a22310.myapplication.Model.UserModel;
import com.example.a22310.myapplication.R;

import java.util.List;

public class UserPostViewModel implements PostListener<Post> {
    private ActivityUserInformationBinding binding;
    private AppCompatActivity activity;
    private int userid;



    public UserPostViewModel(AppCompatActivity activity, ActivityUserInformationBinding binding,int userid){
        this.activity = activity;
        this.binding = binding;
        this.userid = userid;
        binding.setVieww(this);
        setModel(1);

    }

    @Override
    public void onSuccess(Post post) {
        if(post.getCode()!=404){
            List<Post.PostlistBean> postlistBeen =  post.getPostlist();
            if(postlistBeen.size()==0){
                binding.recyclerViewPost.setVisibility(View.GONE);
            }else{
                if(0<postlistBeen.size()&&postlistBeen.size()<3){
                    binding.textBackground.setText("你最近比较懒惰");
                    LinearLayoutManager ms= new LinearLayoutManager(activity);
                    ms.setOrientation(LinearLayoutManager.HORIZONTAL);// 设置 recyclerview 布
                    binding.recyclerViewPost.setLayoutManager(ms);
                    MvvmAdapter adapter = new MvvmAdapter(activity, R.layout.cardview_post_user,
                            postlistBeen, BR.post);
                    binding.recyclerViewPost.setAdapter(adapter);
                }else{
                    binding.textBackground.setVisibility(View.GONE);
                    LinearLayoutManager ms= new LinearLayoutManager(activity);
                    ms.setOrientation(LinearLayoutManager.HORIZONTAL);// 设置 recyclerview 布
                    binding.recyclerViewPost.setLayoutManager(ms);
                    MvvmAdapter adapter = new MvvmAdapter(activity, R.layout.cardview_post_user,
                            postlistBeen, BR.post);
                    binding.recyclerViewPost.setAdapter(adapter);
                }


            }



        }

    }

    @Override
    public void onFailure() {
        Toast.makeText(activity,"连接服务器失败",Toast.LENGTH_LONG).show();
    }

    private void setModel(int page){
       UserModel userModel=new UserModel();
        userModel.postList(UserPostViewModel.this,userid,page);
    }
}
